package com.dc.business.datacollectbusiness.service.impl;

import com.dc.business.datacollectbusiness.entity.Company;
import com.dc.business.datacollectbusiness.entity.CompanyUserRelationShip;
import com.dc.business.datacollectbusiness.mapper.CompanyMapper;
import com.dc.business.datacollectbusiness.mapper.CompanyUserRelationShipMapper;
import com.dc.business.datacollectbusiness.model.Companyarr;
import com.dc.business.datacollectbusiness.service.CompanyService;
import com.dc.business.datacollectbusiness.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CompanyServiceImpl implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private CompanyUserRelationShipMapper companyUserRelationShipMapper;

//    @Override
//    public List<Company> getAllCompany() {
//        return companyMapper.getAllCompany();
//    }


    @Override
    public boolean deleteCompanyById(int companyId) {
        return companyMapper.deleteCompanyById(companyId);
    }

    @Override
    public boolean insertCompany(Company company) {
//        Date date = new Date();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        System.out.println(dateFormat.format(date));

        Map<String,Object> map = new HashMap<>();
        map= ThreadLocalUtil.get();
        company.setCreateBy(map.get("username").toString());

        return companyMapper.insertCompany(company);
    }

    @Override
    public boolean updateCompany(Company company) {
        Map<String,Object> map = new HashMap<>();
        map= ThreadLocalUtil.get();
        company.setUpdateBY(map.get("username").toString());
        return companyMapper.updateCompany(company);
    }

    @Override
    public List<Company> getAllCompanyBytj(Company company) {
        return companyMapper.getAllCompanyBytj(company);
    }

    @Override
    public boolean deleteCompanyByIdall(Companyarr companyarr) {
        boolean b=false;
        String arr[]=companyarr.getArr();
        for (int i = 0; i < arr.length; i++) {
            int result = Integer.parseInt(arr[i]);
            b=companyMapper.deleteCompanyById(result);
        }
        return b;
    }

    @Override
    public Company getAllCompanyByid(int companyId) {
        return companyMapper.getAllCompanyByid(companyId);
    }

    @Override
    public boolean updateCompanyByUserId(Integer userId,int companyId) {
        CompanyUserRelationShip companyUserRelationShip=new CompanyUserRelationShip();
        companyUserRelationShip.setCompanyId(companyId);
        companyUserRelationShip.setUserId(userId);
        return companyUserRelationShipMapper.insertCompanyUserRelationShip(companyUserRelationShip);
    }

    @Override
    public boolean generationParameters(Company company) {

        System.out.println(company);
        return companyMapper.generationParameters(company);

    }
}
